package edu.ustb.efws.pipeline.service;

import edu.ustb.efws.pipeline.domain.IptMaxI;
import edu.ustb.efws.pipeline.mapper.IptMaxIMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 最大电流业务服务
 *
 * @author TS Group
 */
@Service
@Transactional(readOnly = true)
public class IptMaxIService {
    private final IptMaxIMapper mapper;

    @Autowired
    public IptMaxIService(IptMaxIMapper mapper) {
        this.mapper = mapper;
    }

    /**
     * 新增或修改最大电流
     *
     * @param iptId iptId
     * @param month 年月
     * @param maxI 最大电流
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveOrUpdate(int iptId, int month, float maxI){
        IptMaxI t = new IptMaxI();
        t.setIptId(iptId);
        t.setMonth(month);
        t.setMaxI(maxI);
        mapper.insertOrUpdate(t);
    }

    /**
     * 获取最大电流
     *
     * @param iptId 设备编号
     * @param month 年月
     * @return 最大电流
     */
    public Optional<Float> getMaxI(int iptId, int month){
        IptMaxI t = mapper.findOne(iptId, month);
        return t == null? Optional.empty(): Optional.of(t.getMaxI());
    }

    /**
     * 查询多个Ipt最大电流
     *
     * @param iptIds 最大电流
     * @param month 年月
     * @return {@link IptMaxI}集合
     */
    public List<IptMaxI> findIn(List<Integer> iptIds, int month){
        return mapper.findIn(iptIds, month);
    }
}
